<?php
	class Upload extends MY_controller{

		function __construct(){
			parent::__construct();
		}

		function image(){
			$school_user_dir = 'static/uploads/school/system';
			if(!file_exists($school_user_dir)){
                mkdir($school_user_dir,0777);
            }
            $config['upload_path']      = $school_user_dir . '/' . date('Y-m-d');
            if(!file_exists($config['upload_path'])){
                mkdir($config['upload_path'],0777);
            }
	        $config['allowed_types']    = '*';
	        $config['encrypt_name'] = true;
	        $this->load->library('upload', $config);
	        if ( ! $this->upload->do_upload('file'))
	        {
	            $error = array('error' => $this->upload->display_errors());
	            echo json_encode(array("code" => 0));
	        }else
	        {
	            $data = array('upload_data' => $this->upload->data());
	            $result['code'] = 0;
	            $result['data'] = array("src" => '/' . $config['upload_path'] . '/' . $data['upload_data']['raw_name'] . $data['upload_data']['file_ext']);
	            echo json_encode($result);
	        }
		}

		//0、题目，1、难以程度，2、题目类型，3、答案A，4、答案B，5、答案C，6、答案D，7、正确答案，8、答案解析，9、科目，10、章，11、节，12、分类，13、省份
		function upexcel(){
			set_time_limit(0);
			// require_once './third_party/Excel/reader.php';
			require_once './third_party/phpexcel/Classes/PHPExcel/IOFactory.php';
			$objPHPExcel = PHPExcel_IOFactory::load($_FILES['file']['tmp_name']);
			$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
			$numRows = count($sheetData);
			$for_num = 0;
			$excel_data = array();
			$max_data = 101;
			/*if($numRows > $max_data){
				$result['code'] = 201;
	            $result['data'] = array();
	            echo json_encode($result);
	            exit;
			}*/
			if($numRows < $max_data) $max_data = $numRows;
			$zm = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N");
			for ($i = 2; $i <= $max_data; $i++) {
				if(empty($sheetData[$i][$zm[0]])){
					break;
				}
			    for ($j = 0; $j < 14; $j++) {
			    	$excel_data[$for_num][] = $sheetData[$i][$zm[$j]];
			    }
			    $for_num++;
			}
			// var_dump($excel_data);exit;
			if(count($excel_data) > 0){
				foreach ($excel_data as $key => $value) {
					if(empty($value[0]) || empty($value[1]) || empty($value[2]) || empty($value[3]) || empty($value[4]) || empty($value[7]) || empty($value[9]) || empty($value[10]) || empty($value[12])){
						//缺少数据
						// print_r($value);
					}else{
						$time = time();
						$school_id = 0;
						$source_type = 2;
						$subject_id = $category_id = $chapter_id = $type = $difficulty_level = $area_code = 0;
						$subject_text = $analysis = $category_name = $area_name = '';
						$subject_text = $value[0];
						$analysis = $value[8];
						//科目
						$subject = get_field_by_where("b_subject", "id, tk_category_id", "title='{$value[9]}' AND school_id = 0");
						if($subject){
							$subject_id = $subject['id'];
							$category_id = $subject['tk_category_id'];
							//分类名称
							$tk_category_info = get_field_by_where("tk_categorys", "name", "id='{$category_id}' AND school_id = 0");
							if($tk_category_info){
								$category_name = !empty($tk_category_info['name']) ? $tk_category_info['name'] : '';
							}else{
								$this->db->insert("tk_categorys", array("name"=>$value[12], "school_id" => 0, "add_time" => time()));
								$category_id = $this->db->insert_id();
								$category_name = $value[12];
							}
							$chapter_zhang = get_field_by_where("b_subject_chapter", "id", "subject_id='{$subject_id}' AND title = '{$value[10]}'");
							if($chapter_zhang){
								$zhang_id = $chapter_zhang['id'];
							}else{
								$this->db->insert("b_subject_chapter", array("title" => $value[10], "subject_id" => $subject_id, "is_chapter" => 0, "create_on" => $time));
								$zhang_id = $this->db->insert_id();
							}
							$chapter_info = get_field_by_where("b_subject_chapter", "id", "subject_id='{$subject_id}' AND title = '{$value[11]}' AND parent_id = $zhang_id");
							if($chapter_info){
								$chapter_id = $chapter_info['id'];
							}else{
								$this->db->insert("b_subject_chapter", array("title" => $value[11], "subject_id" => $subject_id, "parent_id" => $zhang_id, "is_chapter" => 1, "create_on" => $time));
								$chapter_id = $this->db->insert_id();
							}
							
						}else{
							//查询题库分类
							$tk_category_info = get_field_by_where("tk_categorys", "id", "name='{$value[12]}' AND school_id = 0");
							if(!empty($tk_category_info)){
								$category_id = $tk_category_info['id'];
							}else{
								$this->db->insert("tk_categorys", array("name"=>$value[12], "school_id" => 0, "add_time" => time()));
								$category_id = $this->db->insert_id();
							}
							
							$category_name = $value[12];
							$this->db->insert("b_subject", array("title"=>$value[9], 'school_id' => 0, "create_on" => $time, "tk_category_id" => $category_id));
							$subject_id = $this->db->insert_id();

							//插入章
							$this->db->insert("b_subject_chapter", array("title" => $value[10], "subject_id" => $subject_id, "is_chapter" => 0, "create_on" => $time));

							//插入节
							$this->db->insert("b_subject_chapter", array("title" => $value[11], "subject_id" => $subject_id, "parent_id" => $this->db->insert_id(), "is_chapter" => 0, "create_on" => $time));

							$chapter_id = $this->db->insert_id();
						}
						switch ($value[2]) {
							case '单选题':
								$type = 1;
								break;
							case '多选题':
								$type = 2;
								break;
							case '判断题':
								$type = 3;
								break;
							case '计算分析题':
								$type = 4;
								break;
						}
						switch ($value[1]) {
							case '易':
								$difficulty_level = 1;
								break;
							case '中':
								$difficulty_level = 2;
								break;
							case '难':
								$difficulty_level = 3;
								break;
						}
						$area_info = get_field_by_where("areas", "area_id, area_name", "parent_code=0 AND area_name='{$value[13]}'");
						$area_code = $area_info['area_id'];
						$area_name = $area_info['area_name'];
						$topic_data = array("subject_id" => $subject_id, "category_id" => $category_id, "chapter_id" => $chapter_id, "type" => $type, "difficulty_level" => $difficulty_level, "area_code" => $area_code, "subject_text" => $subject_text, "analysis" => $analysis, "category_name" => $category_name, "area_name" => $area_name, "source_type" => $source_type, "school_id" => $school_id, "add_time" => time());
						$this->db->insert("tk_topics", $topic_data);
						$topic_id = $this->db->insert_id();
						if($topic_id > 0){
							//0、题目，1、难以程度，2、题目类型，3、答案A，4、答案B，5、答案C，6、答案D，7、正确答案，8、答案解析，9、科目，10、章，11、节，12、分类，13、省份
							if(!empty($value[3])){
								$is_right = 0;
								if(stripos($value[7], "A") !== false){
									$is_right = 1;
								}
								$topic_result_data = array("qid"=>$topic_id, "answer_text" => $value[3], "is_right" => $is_right);
								$this->db->insert("tk_topic_result", $topic_result_data);
							}
							if(!empty($value[4])){
								$is_right = 0;
								if(stripos($value[7], "B") !== false){
									$is_right = 1;
								}
								$topic_result_data = array("qid"=>$topic_id, "answer_text" => $value[4], "is_right" => $is_right);
								$this->db->insert("tk_topic_result", $topic_result_data);
							}
							//判断题忽略后面两个答案
							if($type != 3){
								if(!empty($value[5])){
									$is_right = 0;
									if(stripos($value[7], "C") !== false){
										$is_right = 1;
									}
									$topic_result_data = array("qid"=>$topic_id, "answer_text" => $value[5], "is_right" => $is_right);
									$this->db->insert("tk_topic_result", $topic_result_data);
								}
								if(!empty($value[6])){
									$is_right = 0;
									if(stripos($value[7], "D") !== false){
										$is_right = 1;
									}
									$topic_result_data = array("qid"=>$topic_id, "answer_text" => $value[6], "is_right" => $is_right);
									$this->db->insert("tk_topic_result", $topic_result_data);
								}
							}
						}
					}
				}
				$result['code'] = 200;
	            $result['data'] = $excel_data;
	            echo json_encode($result);
			}
		}
	}
